package com.lwj.controller.common;

import com.lwj.entity.RestEntity;
import com.lwj.service.common.file.FileService;
import com.lwj.service.common.file.FileServiceImpl;
import com.lwj.utils.TiKaToolKit;
import com.lwj.vo.common.FileInfoVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 *功能：
 * 1.校验接口权限
 * 2.根据AttConfiguration，校验文件类型，大小，数量
 * 3.保存文件，添加FileManage表记录
 *
 */
@Api(tags = "附件接口")
@RestController()
@RequestMapping("/api/common/file")
public class FileController {
    @Autowired
    private FileService fileService;

    @ApiOperation("上传文件")
    @PostMapping("/upload")
    public RestEntity<FileInfoVo> upload(@RequestParam(value = "file") MultipartFile file, @RequestParam(value = "relationId") String relationId, @RequestParam(value = "code") String code) {
        return RestEntity.success(fileService.upload(file,relationId,code));
    }

    @ApiOperation("删除文件")
    @DeleteMapping("/del")
    public RestEntity del(@RequestParam(value = "fileId") String fileId, @RequestParam(value = "code") String code) {
        fileService.del(fileId,code);
        return RestEntity.success();
    }

    @ApiOperation("下载文件(返回文件流)")
    @GetMapping("/down")
    public byte[] down(@RequestParam(value = "fileId",defaultValue = "aa") String fileId, @RequestParam(value = "size", defaultValue = "0") Integer size, @RequestParam(value = "code") String code) {
        return fileService.down(fileId,size,code);
    }

    @ApiOperation("下载文件")
    @GetMapping("/down2")
    public void down2(@RequestParam(value = "fileId") String fileId, @RequestParam(value = "size", defaultValue = "0") Integer size, @RequestParam(value = "code") String code) {
         fileService.down2(fileId,size,code);
    }

    @ApiOperation("旋转图片")
    @GetMapping("/rotateImage")
    public RestEntity rotateImage(@RequestParam(value = "fileId") String fileId, @RequestParam(value = "angle") Integer angle, @RequestParam(value = "code") String code) {
         fileService.rotateImage(angle,fileId,code);
         return RestEntity.success();
    }

    @ApiOperation("文件类型")
    @PostMapping("/type")
    public RestEntity type(@RequestParam(value = "file") MultipartFile file) {
        String contentType = TiKaToolKit.getMimeType(file);
        return RestEntity.success(contentType);
    }
}
